<template>
  <el-form
      :model="caiPin"
      label-position="right"
      label-width="100px"
      style="padding: 20px;"
  >
    <el-row>
      <h1 style="color: #f58147;font-size: 20px">新增菜品</h1>
    </el-row>
    <el-card style="box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); border-radius: 8px;">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="菜品名称" prop="cpmc">
            <el-input v-model="caiPin.cpmc" placeholder="请输入菜品名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜品价格" prop="cpjg">
            <el-input v-model="caiPin.cpjg" placeholder="请输入菜品价格" clearable />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="菜品图片" prop="cptp">
            <el-upload
                action="/api/upload"
                list-type="picture-card"
                :on-success="handleUploadSuccess"
                :limit="1"
                :on-exceed="handleExceed"
                style="width: 100%;"
            >
              <el-icon name="plus" />
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜品描述" prop="cpms">
            <el-input v-model="caiPin.cpms" placeholder="请描述菜品信息" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-card>

    <el-row style="margin-top: 20px; text-align: center; display: flex;justify-content: center">
      <el-button type="primary" @click="doSave" style="background-color: #f58147; border-color: #f58147;">保存</el-button>
      <el-button @click="doCancel">取消</el-button>
    </el-row>
  </el-form>
</template>


<script setup>
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const caiPin = ref({
  cpmc:'',
  cptp:'',
  cpjg:'',
  cpms:'',
})

// 取消
const doCancel = () => {
  // 触发事件：关闭 dialog
  emits("closeDialog");
}

// 新增
const doSave = async () => {
  const {data} = await axios.post('/api/caiPin', caiPin.value);
  console.log(data);
  if (data.code === 200) {
    emits("flushPage");				// 触发事件： 刷新页面
    emits("closeDialog");			// 触发事件：关闭 dialog
    ElMessage.success("操作成功！");
  } else {
    ElMessage.error("操作失败！");
  }
}

// 文件上传成功回调
const handleUploadSuccess = (response, file) => {
  if (response.code === 200) {
    caiPin.value.cptp = response.data; // 假设返回的图片路径在 response.data 中
    ElMessage.success("上传成功");
  } else {
    ElMessage.error("上传失败");
  }
};

// 文件上传超出限制
const handleExceed = () => {
  ElMessage.warning("最多只能上传一张图片");
};

</script>

<style scoped>
.el-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.el-form-item__content {
  width: 100%;
}

.el-input__inner {
  border-radius: 6px;
  border: 1px solid #e4e7ed;
  padding: 10px;
  transition: all 0.3s ease;
}

.el-input__inner:focus {
  border-color: #f58147;
  box-shadow: 0 0 5px rgba(245, 129, 71, 0.3);
}

.el-button--primary {
  background-color: #f58147;
  border-color: #f58147;
  transition: all 0.3s ease;
}

.el-button--primary:hover {
  background-color: #e0743f;
  border-color: #e0743f;
}
</style>